package org.xdemo.example.shiro.controller;

import com.alibaba.druid.support.json.JSONUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.xdemo.example.Utils.MD5Util;
import org.xdemo.example.shiro.exception.BusinessException;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by kz on 2017/8/24.
 */

@Controller
public class loginController {

    @RequestMapping("")
    public String welcome(){
        return "/index";
    }

    @RequestMapping("/toindex")
//    public ModelAndView getIndex(HttpServletRequest request) throws Exception {
//        ModelAndView mav = new ModelAndView("index");
//        return mav;
//    }
    public String getIndex(HttpServletRequest request) throws Exception {
        return "/index";
    }

    @RequestMapping("/exceptionForPageJumps")
    public ModelAndView exceptionForPageJumps(HttpServletRequest request) throws Exception {
        throw new BusinessException("空指针");
    }

    @RequestMapping(value="/businessException", method= RequestMethod.POST)
    @ResponseBody
    public String businessException(HttpServletRequest request) {
        throw new BusinessException("空指针");
    }

    @RequestMapping(value="/otherException", method=RequestMethod.POST)
    @ResponseBody
    public String otherException(HttpServletRequest request) throws Exception {
        throw new Exception();
    }

    //跳转到登录页面
    @RequestMapping("/tologin")
    public String tologin() throws Exception {
//        ModelAndView mav = new ModelAndView("remoteUI/testHiRMULogin");
//        return mav;
        return "loginout/page_HiRUILogin";
    }

    //跳转到登录页面
    @RequestMapping("/tologout")
    public String tologout() throws Exception {
//        ModelAndView mav = new ModelAndView("remoteUI/testHiRMULogin");
//        return mav;
        return "loginout/testHiRMULogout";
    }
    //跳转到登录成功页面
    @RequestMapping("/loginsuccess")
//    public ModelAndView loginsuccess() throws Exception {
//        ModelAndView mav = new ModelAndView("remoteUI/testform1");
//        return mav;
//    }
    public String loginsuccess(){
        return "forward:/toindex";
    }

    @RequestMapping("/newPage")
    public ModelAndView newPage() throws Exception {
        ModelAndView mav = new ModelAndView("newPage");
        return mav;
    }

    @RequestMapping("/newPageNotAdd")
    public ModelAndView newPageNotAdd() throws Exception {
        ModelAndView mav = new ModelAndView("newPageNotAdd");
        return mav;
    }

    /**
     * 验证用户名和密码
     * @param
     * @return
     */
    @RequestMapping(value="/checkLogin",method=RequestMethod.POST)
    @ResponseBody
    public String checkLogin(String username,String password) {
        Map<String, Object> result = new HashMap<String, Object>();
        try{
            UsernamePasswordToken token = new UsernamePasswordToken(username, MD5Util.getMD5(password));
            Subject currentUser = SecurityUtils.getSubject();
            if (!currentUser.isAuthenticated()){
                //使用shiro来验证
                token.setRememberMe(true);
                currentUser.login(token);//验证角色和权限
            }
        }catch(Exception ex){
            throw new BusinessException("验证失败");
        }
        result.put("success", true);
        return JSONUtils.toJSONString(result);
    }

    /**
     * 退出登录
     */
    @RequestMapping(value="/logout",method=RequestMethod.POST)
    @ResponseBody
    public String logout() {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("success", true);
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
        return JSONUtils.toJSONString(result);
    }
}
